<?php
class ModelSaleGuest extends Model {
	public function addGuest($data) {
      	$this->db->query("INSERT INTO " . DB_PREFIX . "guest SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
      	
      	$guest_id = $this->db->getLastId();
	}
	
	public function editGuest($guest_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "guest SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', status = '" . (int)$data['status'] . "' WHERE guest_id = '" . (int)$guest_id . "'");
	}
	
	public function deleteGuest($guest_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "guest WHERE guest_id = '" . (int)$guest_id . "'");
	}
	
	public function getGuest($guest_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "guest WHERE guest_id = '" . (int)$guest_id . "'");
	
		return $query->row;
	}
		
	public function getGuests($data = array()) {
		$sql = "SELECT *, CONCAT(c.firstname, ' ', c.lastname) AS name, cg.name AS guest_group FROM " . DB_PREFIX . "guest c LEFT JOIN " . DB_PREFIX . "guest_group cg ON (c.guest_group_id = cg.guest_group_id)";

		$implode = array();
		
		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$implode[] = "LCASE(CONCAT(c.firstname, ' ', c.lastname)) LIKE LCASE('%" . $this->db->escape($data['filter_name']) . "%')";
		}
		
		if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
			$implode[] = "c.email LIKE '%" . $this->db->escape($data['filter_email']) . "%'";
		}
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$implode[] = "c.status = '" . (int)$data['filter_status'] . "'";
		}	
		
		if (isset($data['filter_ip']) && !is_null($data['filter_ip'])) {
			$implode[] = "c.guest_id IN (SELECT guest_id FROM " . DB_PREFIX . "guest_ip WHERE ip = '" . $this->db->escape($data['filter_ip']) . "')";
		}	
				
		if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
			$implode[] = "DATE(c.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}
		
		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}
		
		$sort_data = array(
			'name',
			'c.email',
			'c.status',
			'c.ip',
			'c.date_added'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY name";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);
		
		return $query->rows;	
	}

	public function getTotalGuests($data = array()) {
      	$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "guest";
		
		$implode = array();
		
		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}
		
		if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
			$implode[] = "LCASE(email) LIKE '%" . $this->db->escape($data['filter_email']) . "%'";
		}
				
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$implode[] = "status = '" . (int)$data['filter_status'] . "'";
		}
				
		if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
			$implode[] = "DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}
		
		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}
				
		$query = $this->db->query($sql);
				
		return $query->row['total'];
	}				
}
?>